$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + "sys/frozenTerminal/list",
        datatype: "json",
        colModel: [
            {label: 'id', name: 'countId', width: 80, align: 'center', sortable: false},
            {label: 'SN', name: 'sn', width: 80, align: 'center', sortable: false},
            {label: '冻结人', name: 'name', width: 45, align: 'center', sortable: false},
            {label: '联系方式', name: 'phone', width: 45, align: 'center'},
            {
                label: '冻结金额',
                name: 'amount',
                width: 45,
                align: 'center',
                sortable: false,
                formatter: function (value, options, row) {
                    return value.toFixed(2);
                }
            },
            {label: '冻结时间', name: 'createTime', width: 45, align: 'center', sortable: false},
            {
                label: '解冻状态', name: 'state', width: 75, formatter: function (value, options, row) {
                    return value == 1 ? "已解冻" : "未解冻";
                }, align: 'center', sortable: false
            },
            {
                label: '最近解冻时间', name: 'thawTime', width: 75, formatter: function (value, options, row) {
                    return value == null ? "-" : value;
                }, align: 'center', sortable: false
            },
            {
                label: '解冻许可', name: 'thawFlag', width: 75, formatter: function (value, options, row) {

                    return value == 0 ? '<span class = "btn btn-sm btn-warning" onclick = "thawFlag(' + row.countId + ',' + value + ')"> 不允许 </span>' :

                        '<span class = "btn btn-sm btn-success" onclick = "thawFlag(' + row.countId + ',' + value + ')"> 允许 </span>';


                }, align: 'center', sortable: false
            },
        ],
        viewrecords: true,
        height: 375,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        multiboxonly: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });

    vm.bindType();

});

function thawFlag(id, flag) {

    let str = flag == 1 ? "确定禁止该机器的继续解冻么?" : "确定允许该机器继续解冻么?";
    confirm(str, function () {
        flag = flag == 1 ? 0 : 1;

        $.get(baseURL + "sys/frozenTerminal/thawFlag/" + id + "/" + flag, r => {
                if (r.code == 0) {
                    layer.msg("操作成功")
                    vm.reload();
                } else {
                    layer.msg("操作失败")
                }
            }
        )


    })


}

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            name: null,
            phone: null,
            sn: null
        },
        showList: true,
        showUpdate: false,
        showAdd:false,
        title: null,
        frozen: {
            countId: null,
            sn: null,
            agentId: null,
            amount: null,
            deptId: null,
            state: null,
            createTime: null,
            thawTime: null
        },
        newFrozen:{
            name:null,
            phone:null,
            sn:null,
            terminalType:0,
            model:null
        },
        terminalTypes:[]
    },
    methods: {
        query: function () {
            vm.reload();
        },
        refresh: function () {
            vm.q = {
                startSn: null,
                endSn: null,
                merchantCode: null,
                activation: 0,
                terminalType: 0,
                ownName: null,
                reapName: null,
                report: 0
            };
            vm.reload();
        },
        reload: function () {
            vm.showList = true;
            vm.showUpdate = false;
            vm.showAdd=false;
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'name': vm.q.name,
                    'phone': vm.q.phone,
                    'sn': vm.q.sn
                },
                page: 1
            }).trigger("reloadGrid");
        }, getById: function () {
            let rowid = getSelectedRow();
            let row = $("#jqGrid").jqGrid('getRowData', rowid);
            if (rowid == null) {
                return;
            }

            $.get(baseURL + "sys/frozenTerminal/getById/" + row.countId, r => {
                vm.frozen = r.frozen;
                vm.showList = false;
                vm.showUpdate = true;
                vm.showAdd=false;
            })
        }, update() {
            $.ajax({
                url: baseURL + "sys/frozenTerminal/update",
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(vm.frozen),
                success: function (r) {
                    if (r.code == 0) {
                        alert(r.msg);
                        vm.reload();
                    } else {
                        alert(r.msg)
                    }
                }
            })
        },
        add(){
            vm.newFrozen={
                name:null,
                phone:null,
                sn:null,
                terminalType:0,
                model:null
            }
            vm.showUpdate=false;
            vm.showList=false;
            vm.showAdd=true;

        }
        ,save(){
            $.ajax({
                url: baseURL + "sys/frozenTerminal/add",
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(vm.newFrozen),
                success: function (r) {
                    if (r.code == 0) {
                        alert(r.msg);
                        vm.reload();
                    } else {
                        alert(r.msg)
                    }
                }
            })

        },bindType:function () {
                $.getJSON(baseURL + "sys/terminalType/list", function (r) {
                    vm.$set(vm.terminalTypes, 0, {
                        terminalType: 0,
                        terminalName: "请选择类型"
                    });
                    for (var i = 0; i < r.page.length; i++) {
                        vm.$set(vm.terminalTypes, (i + 1), r.page[i]);
                    }
                })

        }
    }
});

